package com.bjpowernode.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.excel.EasyExcel;
import com.bjpowernode.common.constant.Constant;
import com.bjpowernode.entity.TCustomer;
import com.bjpowernode.entity.query.BaseQuery;
import com.bjpowernode.entity.query.CustomerExcel;
import com.bjpowernode.entity.query.CustomerQuery;
import com.bjpowernode.entity.vo.CustomerListVO;
import com.bjpowernode.mapper.TClueMapper;
import com.bjpowernode.mapper.TCustomerMapper;
import com.bjpowernode.service.CustomerService;
import com.bjpowernode.util.LoginInfoUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.servlet.ServletOutputStream;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
@Slf4j
@RequiredArgsConstructor
public class CustomerServiceImpl implements CustomerService {

    private final TClueMapper clueMapper;

    private final TCustomerMapper customerMapper;

    /**
     * 将线索转换为客户
     * @param customerQuery
     * @return
     *
     * 首先要看线索是否已经成为客户
     * 在线索成为客户之后要改变线索状态
     */
    @Override
    @Transactional
    public int convertCustomer(CustomerQuery customerQuery) {
        // 检查线索是否存在并且未被转换为客户
        Integer flag = clueMapper.checkClueState(customerQuery.getClueId());
        if (flag == 0) {// 线索不存在或已被转换为客户
            return -1; // 返回-1表示线索已存在
        }

        //更新线索状态
        clueMapper.updateClueState(customerQuery.getClueId(), -1); // 1表示已转换为客户

        //保存客户信息
        TCustomer customer = new TCustomer();
        BeanUtil.copyProperties(customerQuery, customer);
        customer.setCreateTime(new Date());
        customer.setCreateBy(LoginInfoUtil.getCurrentLoginUser().getId());


        return customerMapper.insert(customer);
    }

    @Override
    public PageInfo<CustomerListVO> getCustomerByPage(Integer current) {
        PageHelper.startPage(current, Constant.PAGE_SIZE);
        return new PageInfo<>(customerMapper.getCustomerByPage(new BaseQuery()));
    }

    @Override
    public void exportExcel(ServletOutputStream outputStream, List<String> idList, Integer pageNum){
        List<CustomerExcel> customerExcelList = new ArrayList<>();
        if (pageNum != null && pageNum > 0) {
            PageHelper.startPage(pageNum, Constant.PAGE_SIZE);
            customerExcelList = customerMapper.getCustomerExcelList(new BaseQuery(), null);
        } else {
            customerExcelList = customerMapper.getCustomerExcelList(new BaseQuery(), idList);
        }
        EasyExcel.write(outputStream, CustomerExcel.class)
                .sheet("客户信息")
                .doWrite(customerExcelList);
    }

    @Override
    public TCustomer getCustomerById(Integer id) {
        TCustomer customer = customerMapper.getCustomerById(id);
        log.info("查询到的客户信息: {}", customer);
        return customer;
    }
}
